<template>
  <div style="width: 400px; margin: 120px auto;">
    <div style="font-size: 30px; text-align: center; padding: 30px 0; margin-left: 88px;">欢迎注册</div>
    <el-form ref="registerForm" :rules="rules" :model="registerForm" label-width="120px">
      <el-form-item label="账号" prop="userAccount" class="custom-label">
        <el-input v-model="registerForm.userAccount"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="userPassword" class="custom-label">
        <el-input type="password" v-model="registerForm.userPassword"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPassword" class="custom-label">
        <el-input type="password" v-model="registerForm.checkPassword"></el-input>
      </el-form-item>
      <el-form-item class="center-buttons">
        <el-button type="primary" @click="submitForm()">注册</el-button>
        <el-button type="danger" @click="clickRouter">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { register } from '@/http/user'
export default {
  data() {
    return {
      registerForm: {
        checkPassword: "",
        userAccount: "",
        userPassword: ""
      },
      rules: {
        checkPassword: [
          { required: true, message: '请输入确认密码' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符' }
        ],
        userPassword: [
          { required: true, message: '请输入密码' },
          { min: 6, max: 12, message: '长度在 6 到 12 个字符' }
        ],
        userAccount: [
          { required: true, message: '请输入账号名称' },
          { min: 3, max: 12, trigger: 'blur' }
        ],
      },
      options: [{
        value: 0,
        label: '男'
      }, {
        value: 1,
        label: '女'
      }],
      departmentOption: []
    };
  },
  mounted() {

  },
  methods: {
    submitForm() {
      this.$refs.registerForm.validate(async (valid) => {
        if (valid) {
          const { code, data, message } = await register(this.registerForm)
          if (code == '0') {
            this.$message.success('注册成功！');
            this.clickRouter()
          } else {
            this.$message.error(message);
          }
        } else {
          this.$message.error('注册信息校验失败！');
          return false;
        }
      });
    },
    clickRouter() {
      this.$router.push('/login')
    }
  }
};
</script>

<style>
.center-buttons {
  display: flex;
  justify-content: center;
}

.custom-label .el-form-item__label {
  color: #fff; /* 你可以替换为你想要的颜色 */
  font-size: 18px;
}
</style>
